<?php

namespace app\sys\controller;

use think\Db;

// 后台 邮费差价管理
class Postage extends Admin
{
    // 邮费差价列表
    public function index()
    {
        $admin_info = session('admin_info');
        $date = input('param.date', '');
        $page = input('param.page', 1);
        $page = $page <= 0 ? 1 : $page;
        $limit = input('param.limit', 100);
        $order_field = input('param.field', '');
        $order_type = input('param.order', 0);
        $query = [];
        // 供应商只能看到自己的
        if (6 == $admin_info['permission']) {
            $query['admin_id'] = $admin_info['id'];
        }
        $order = ['ct' => -1];
        if ($order_field && $order_type) {
            $order[$order_field] = 'asc' == $order_type ? 1 : -1;
        }
        $list = Db::name('postage')->where($query)->page($page)->limit($limit)->order($order)->select();
        $count = Db::name('postage')->where($query)->count();

        return view('index', ['list' => $list]);
    }

    // 邮费差价 添加\编辑
    public function edit()
    {
        if (request()->isPost()) {
            $admin_info = session('admin_info');
            $data = input('param.', '');
            $row = [];
            $row['name'] = '顺丰快递'; //strval($data['name']);
            foreach ($data['table'] as $key => $value) {
                $value['area'] = strval($value['area']);
                $value['first_weight'] = round($value['first_weight'], 2);
                $value['continue_weight'] = round($value['continue_weight'], 2);
                $row['table'][] = $value;
            }
            // 这版本定死只有顺丰加价 其他包邮
            $row['express_code'] = 'SF';
            $id = $data['id'];
            $row['admin_id'] = strval($admin_info['id']);
            //存在修改 不存在添加
            Db::name('postage')->where(['id' => $id, 'express_code' => 'SF'])->update($row, ['upsert' => true]);

            return success();
        }

        $id = input('param.id', '');
        $data = Db::name('postage')->find($id);
        $data['table'] = json_encode($data['table'] ? $data['table'] : []);

        return view('edit', ['info' => $data]);
    }

    // 邮费差价删除
    public function del()
    {
        if (request()->isPost()) {
            $admin_info = session('admin_info');
            if ($admin_info['permission'] < 6) {
                return error('你的权限不够');
            }
            $id = input('param.id', '');
            Db::name('postage')->delete($id);

            return success();
        }

        return error();
    }

    // 设置供应商 退货地址
    public function returnAddress()
    {
        $admin_info = session('admin_info');
        if (6 != $admin_info['permission']) {
            return error('你的权限不够');
        }
        if (request()->isPost()) {
            $data = input('param.', '');
            $row = [];
            $row['admin_id'] = strval($admin_info['id']);
            $row['consignee'] = strval($data['consignee']);
            $row['phone'] = strval($data['phone']);
            if (!checkPhone($row['phone'])) {
                return error('请正确填写手机号');
            }
            $row['address'] = strval($data['address']);
            $row['ct'] = time();
            //存在修改 不存在添加
            Db::name('return_address')->where('admin_id', $row['admin_id'])->update($row, ['upsert' => true]);

            return success();
        }
        $data = Db::name('return_address')->where('admin_id', $admin_info['id'])->find();

        return view('returnAddress', $data);
    }
}
